home *** CD-ROM | disk | FTP | other *** search
- unit ServerMainFormUnit;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- Menus, StdCtrls, ExtCtrls;
-
- type
- TForm1 = class(TForm)
- Timer1: TTimer;
- Memo1: TMemo;
- MainMenu1: TMainMenu;
- LaunchChildApp1: TMenuItem;
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure LaunchChildApp1Click(Sender: TObject);
- procedure Timer1Timer(Sender: TObject);
- private
- Mailslot: THandle;
- end;
-
- var
- Form1: TForm1;
-
- const
- MailslotNameFixedReadPrefix = '\\.\mailslot\';
- MailslotName = 'SampleMailslot';
- MailslotReadName = MailslotNameFixedReadPrefix + MailslotName;
- MailslotMaxSize = 0; //any size
- MailslotTimeout = 0; //don't wait
-
- {$ifdef Ver90}
- type
- //This exception class did not exist in Delphi 2
- EWin32Error = class(Exception);
-
- const
- //Constant not defined in Delphi 2
- Mailslot_No_Message = -1;
- {$endif}
-
- implementation
-
- {$R *.DFM}
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Mailslot := CreateMailslot(
- MailslotReadName, MailslotMaxSize, MailslotTimeout, nil);
- if Mailslot = Invalid_Handle_Value then
- raise EWin32Error.Create('Unable to create mailslot');
- end;
-
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- CloseHandle(Mailslot);
- end;
-
- procedure TForm1.LaunchChildApp1Click(Sender: TObject);
- const
- ChildApp = 'ClientMailslot.Exe';
- var
- SI: TStartupInfo;
- PI: TProcessInformation;
- begin
- GetStartupInfo(SI);
- if not CreateProcess(nil, ChildApp, nil,
- nil, False, 0, nil, nil, SI, PI) then
- raise EWin32Error.Create('Unable to launch ' + ChildApp);
- end;
-
- procedure TForm1.Timer1Timer(Sender: TObject);
- var
- NextMsgSize, BytesRead: DWord;
- Msg: String;
- begin
- if not GetMailslotInfo(
- Mailslot, nil, NextMsgSize, nil, nil) then
- begin
- Timer1.Enabled := False;
- raise EWin32Error.Create('Cannot get mailslot information')
- end;
- //Check there is a message to read
- if NextMsgSize <> Mailslot_No_Message then
- begin
- //Allocate string size as required and set length
- SetLength(Msg, NextMsgSize);
- //Read the data into the string variable
- if not ReadFile(Mailslot, Msg[1], NextMsgSize, BytesRead, nil) then
- begin
- Timer1.Enabled := False;
- raise EWin32Error.Create('Cannot read from mailslot');
- end;
- Memo1.Text := Msg
- end
- end;
-
- end.
-